package com.wb.sina;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import weibo4j.Oauth;
import weibo4j.Timeline;
import weibo4j.Weibo;
import weibo4j.http.AccessToken;
import weibo4j.model.Paging;
import weibo4j.model.ShortUrl;
import weibo4j.model.ShortUrlWapper;
import weibo4j.model.Status;
import weibo4j.model.StatusWapper;
import weibo4j.model.WeiboException;
import weibo4j.publicapi.ShortUrlUtil;
import weibo4j.util.BareBonesBrowserLaunch;

import com.wb.dao.UserDao;
import com.wb.model.Post;
import com.wb.model.User;

/**
 * 封装了获取微博以及对微博数据的处理
 * @author Administrator
 *
 */

public class WpSina {
	
	private AccessToken accessToken;
	private Weibo weibo;
	private Timeline timeline = new Timeline();
	private List<User> listUsers;
	
	
	private UserDao userDao = new UserDao();
	
	private int count = 100; //每页条数
	private int page;  //页数
	/*
	 * 初始化wpsina
	 */
	

	public void init(){
		Oauth oauth = new Oauth();
        try {
			BareBonesBrowserLaunch.openURL(oauth.authorize("code"));
		} catch (WeiboException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        System.out.print("Hit enter when it's done.[Enter]:");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String code = null;
		try {
			code = br.readLine();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
       
        System.out.println("code: " + code);
        try{
        		accessToken = oauth.getAccessTokenByCode(code);
                System.out.println(accessToken);
                
                weibo = new Weibo();
                weibo.setToken(accessToken.getAccessToken());
                
                timeline = new Timeline();
                
                listUsers = this.userDao.listUsers();
                
        } catch (WeiboException e) {
                if(401 == e.getStatusCode()){
               
                        System.out.println("Unable to get the access token.");
                        
                }else{
                        e.printStackTrace();
                }
        }
	}
	
	/**
	 * 返回postList
	 */
	
	public List<Post> listPost(int uid){
		
		List<Post> listPost = new ArrayList();
		
		Paging p = new Paging();
		p.setPage(1);
		p.setCount(1);
		
		StatusWapper statusWapper = null;
		try {
			statusWapper = this.timeline.getUserTimelineByUid(Integer.toString(uid), p, Integer.valueOf(0),Integer.valueOf(0));
			long totalNum = statusWapper.getTotalNumber();
			if(totalNum > 0){
				 if(totalNum%count == 0){
					 page = (int)(totalNum/count);
				 }else{
					 page = (int)(totalNum/count) + 1;
				 }
				 
					
					p.setCount((int)count);
					
					for(int i = 0; i < page; i++){
						if(i > 0){
							break;
						}
						p.setPage(i+1);
						statusWapper = this.timeline.getUserTimelineByUid(Integer.toString(uid), p, Integer.valueOf(0),Integer.valueOf(0));
						List<Status> listStatus = statusWapper.getStatuses();
						
						//形成listPost
						BufferedWriter w = new BufferedWriter(new FileWriter(new File("s.txt")) );
						for(Status s: listStatus){
							List<String> stList = ShortUrlUtil.shortStr(s.getText());
							String link ="短链接：";
							if(!stList.isEmpty()){
								for(String m : stList){
									link += m;
								}
							}
							System.out.println(link);
							w.write(s.getText());
							w.newLine();
							w.flush();
						}
						
						w.close();
						
					}
				 
			}
			
		} catch (WeiboException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return listPost;
		
	}
	
	/**
	 * status 转换成post
	 * @return
	 */
	public Post ConvertStatus(Status status){
		Post p =  new Post();
		p.setContent(status.getText());
		p.setImgnet(status.getOriginalPic());
		p.setWeiid(status.getIdstr());
		
		List<String> listUrl = ShortUrlUtil.shortStr(status.getText());
		try {
			ShortUrlWapper shortUrlWapper = ShortUrlUtil.expand(listUrl);
			List<ShortUrl> listShortUrl = shortUrlWapper.getShortUrls();
			for(ShortUrl sh : listShortUrl){
				if(sh.getType() == 1){
					p.setVideo(sh.getUrl_long());
				}else if(sh.getType() == 2){
					p.setMusic(sh.getUrl_long());
				}
				
			}
			
		} catch (WeiboException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return p;
	}
	
	public long getCount() {
		return count;
	}

	public void setCount(int count) {
		if(count > 100){
			count =100;
			System.out.println("*****************每页数量(count)不能大于100,重置为 100***************");
		}
		this.count = count;
	}
	
	
}
